package scale.GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;

import scale.data.Scale;

public class WeightGUI {

	private ArrowText AT;
	private TextField T;
	private Keypad b;
	private Scale scale;
	private Brutto bo;

	
	public WeightGUI(Scale sc) {
		this.scale = sc;
		JFrame f = new JFrame("Scale Simulator - Group 15");
		f.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		

		T = new TextField();
		
		T.T.setText("Information Display");

		f.add(T,c);

		AT = new ArrowText();

		c.gridx = 0;
		c.gridy = 1;
		AT.setBackground(Color.black);
		f.add(AT,c);

		UpArrows UA = new UpArrows() {

			@Override
			public void controlAction(int i) {

				scale.rmResponse("" + i, T.getText1());
				clearTextField();
			}




		};
		c.gridx = 0;
		c.gridy = 2;

		f.add(UA,c);


		b = new Keypad(T) {
			String s = "";
			@Override
			public void action(int t) {
				s += Integer.toString(t);
				T.updateText1(s);
			}

			@Override
			public void actionNonInt(String t) {
				if(!t.equals("C")){
					s += t;
					T.updateText1(s);
				}
				else 
					s = "";
				T.updateText1(s);
			}


		};

		c.gridx = 6;
		c.gridy = 0;
		f.add(b,c);
		
		bo = new Brutto(scale);
		c.gridx = 6;
		c.gridy = 2;
		
		f.add(bo,c);
		
		f.setBackground(Color.black);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.pack();
		f.setLocation((dim.width / 2) - (f.getSize().width/2), (dim.height / 2 - f.getSize().height/2 ));
		f.setVisible(true);
		f.setResizable( false );
	}
	
	public void setBrutto(double d){
		bo.setBrutto(d);
		updateNettoText();
	}
	
	public void setArrowText(int i, String s){
		AT.updateText(i, s);
	}
	
	public void setTextField(String s){
		T.updateText(s);
	}
	
	public void updateNettoText(){
		T.T2.setText(""+(scale.getBrutto()-scale.getTara())+" kg.");
		
	}
	
	public void clearTextField(){
		T.updateText("");
		b.actionNonInt("C");
	}

}
